package com.testing.sunnycommon.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.testing.sunnycommon.model.Result;
import com.testing.sunnycommon.model.SunnyNotice;
import com.testing.sunnycommon.service.SunnyNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Classname SunnyNoticeController
 * @Description 类型说明
 * @Date 2023/6/14 21:01
 * @Created by 特斯汀Roy
 */
@RestController
@RequestMapping("/sunnyNotice")
public class SunnyNoticeController {

    @Autowired
    SunnyNoticeService sunnyNoticeService;

    @GetMapping
    public Result<List<SunnyNotice>> listSunnyNotices() {
        return Result.success(sunnyNoticeService.list());
    }


    /**
     * 批量更新消息，如果修改一条，也需要放到数组中进行操作
     * @param sunnyNoticeList
     * @return
     */
    @PutMapping
    public Result updateSunnyNotices(@RequestBody List<SunnyNotice> sunnyNoticeList) {
        boolean result = sunnyNoticeService.updateBatchById(sunnyNoticeList);
        if (result) {
            return Result.success("标记通知消息已读成功");
        } else {
            return Result.fail(1020, "标记通知消息已读失败", "标记通知消息已读失败");
        }
    }


    @PutMapping("/readAll")
    public Result updateAllSunnyNoticeRead(){
        sunnyNoticeService.update(new LambdaUpdateWrapper<SunnyNotice>()
                .set(SunnyNotice::getIsRead,true));
        return Result.success("更新所有信息成功");
    }


    /**
     * 提供要删除的消息的id列表，完成删除，可以批量和单个删除都行。
     * @param ids
     * @return
     */
    @DeleteMapping("/{ids}")
    public Result deleteSunnyNotices(@PathVariable List<Long> ids) {
        boolean result = sunnyNoticeService.removeBatchByIds(ids);
        if (result) {
            return Result.success("删除通知消息成功");
        } else {
            return Result.fail(1030, "删除通知消息失败", "删除通知消息失败");
        }
    }


    /**
     * 清空所有通知消息
     * @return
     */
    @DeleteMapping
    public Result deleteAllNotices(){
        boolean remove = sunnyNoticeService.remove(null);
        if(remove){
            return Result.success("删除所有通知消息成功");
        }else{
            return Result.fail(1031,"删除所有通知消息失败","删除所有通知消息失败");
        }
    }




}
